package com.box.service.search.impl;

import java.lang.reflect.Type;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.DbBlog;
import com.box.entity.solrjItem.SearchBlog;
import com.box.service.search.SearchService;
import com.box.service.solrj.SolrjService;
import com.box.utils.BaiduPageView;
import com.box.utils.PageView;
import com.box.utils.ResponseResult;
import com.google.gson.reflect.TypeToken;

@Service
public class SearchServiceImpl implements SearchService {
	
	@Autowired
	private SolrjService solrjService;

	public PageView search(String queryString,String page) throws Exception {
		// TODO Auto-generated method stub
		
		Integer rows = 50;
		  //创建查询条件
				SolrQuery query=new SolrQuery();
				//设置查询条件   如果queryString为空返回查不到结果
				query.setQuery(queryString);
				int pageint = Integer.parseInt(page);
				//设置分页条件
				query.setStart((pageint -1)*rows);
				query.setRows(rows);
				//设置默认搜索域,普遍为title进行默认搜索
				query.set("df", "blog_title");
				//设置高亮
				query.setHighlight(true);
				query.addHighlightField("blog_title");
				//设置高亮简单样式
				query.setHighlightSimplePre("<font class=\"skcolor_ljg\">");
				query.setHighlightSimplePost("</font>");
				
				PageView searchBySolrj = solrjService.searchBySolrj(query);
				//计算总页数
				Long recordCount = searchBySolrj.getRecordCount();
				int pageCount=(int) (recordCount/rows);
				if(recordCount%rows>0){
					pageCount++;
				}
				BaiduPageView<SearchBlog> pageview=new BaiduPageView<SearchBlog>(pageint, searchBySolrj.getBlogList().size(), searchBySolrj.getBlogList());
				Type listType = new TypeToken<BaiduPageView<SearchBlog>>(){}.getType();
				searchBySolrj .setPageCount(pageCount);
				searchBySolrj .setCurPage(pageint);
				searchBySolrj.setBaidu(pageview);
				searchBySolrj.setListType(listType);
		return searchBySolrj;
	}

	public ResponseResult updateSearchBlogById(Integer blogId) throws Exception {
		// TODO Auto-generated method stub
		return solrjService.updateSearchBlogById(blogId);
	}

	@Override
	public ResponseResult deletById(Integer blogId) throws Exception {
		return solrjService.deleteSearchBlogById(blogId);
	}
}
